package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.bean.WithDrawBankBean;
import com.lc.luzhengtang.citysection.EasySideBarBuilder;
import com.lc.luzhengtang.conn.ConnVerificationCode2;
import com.lc.luzhengtang.conn.ConnWithDrawBank;
import com.lc.luzhengtang.conn.ConnWithDrawSubmit;
import com.lc.luzhengtang.util.LovelyToast;
import com.lc.luzhengtang.util.Util;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/5/5.
 */
public class WithDrawActivity extends BaseActivity {


    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_withdraw)
    TextView textWithdraw;
    @BindView(R.id.layout_yinlian)
    LinearLayout layoutYinlian;

    @BindView(R.id.layout_weixin)
    LinearLayout layoutWeixin;

    @BindView(R.id.textView2)
    TextView textView2;

    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.text_money)
    TextView textMoney;
    @BindView(R.id.img_yinlian)
    ImageView imgYinlian;
    @BindView(R.id.img_alipay)
    ImageView imgAlipay;
    @BindView(R.id.img_wx)
    ImageView imgWx;

    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.layout_union)
    LinearLayout layoutUnion;
    @BindView(R.id.layout_alipay)
    LinearLayout layoutAlipay;
    @BindView(R.id.layout_alipay_include)
    LinearLayout layoutAlipayInclude;
    @BindView(R.id.checkbox_zuhe)
    CheckBox checkboxZuhe;
    @BindView(R.id.text_zuhe)
    TextView textZuhe;
    @BindView(R.id.layout_city)
    LinearLayout layoutCity;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.edit_count)
    EditText editCount;
    @BindView(R.id.edit_recount)
    EditText editRecount;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.text_kaihucity)
    TextView textKaihucity;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.editText_alipay)
    EditText editTextAlipay;
    @BindView(R.id.editText_realipay)
    EditText editTextRealipay;
    @BindView(R.id.text_withdraw_record)
    TextView textWithdrawRecord;
    @BindView(R.id.text_title)
    TextView textTitle;


    private ArrayList<String> mList = new ArrayList<String>();

    List<WithDrawBankBean.DataBean> dataBeanList = new ArrayList<>();

    private String types = "1";
    String getTypes = "1";

    //城市
    String city;

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.bind(this);
        getTixianPersonalData();
        checkboxZuhe.setChecked(true);
        checkboxZuhe.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    getTypes = "1";
                } else {
                    getTypes = "2";
                }
            }
        });
    }

    String myCode;

    @OnClick({R.id.img_news, R.id.text_withdraw, R.id.layout_yinlian, R.id.layout_alipay, R.id.layout_weixin, R.id.editText, R.id.textView2, R.id.img_yinlian, R.id.img_alipay, R.id.img_wx, R.id.btn_submit, R.id.text_sendbtn, R.id.text_zuhe, R.id.layout_city, R.id.text_kaihucity, R.id.text_withdraw_record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_withdraw:
                break;
            case R.id.layout_yinlian:
                types = "1";
//                textTitle.setText("银行卡");
                layoutUnion.setVisibility(View.VISIBLE);
                layoutAlipayInclude.setVisibility(View.GONE);
                imgYinlian.setImageResource(R.mipmap.checkboxselected);
                imgAlipay.setImageResource(R.mipmap.checkboxunselected);
                imgWx.setImageResource(R.mipmap.checkboxunselected);
                break;
            case R.id.layout_alipay:
//                textTitle.setText("支付宝");
                layoutUnion.setVisibility(View.GONE);
                layoutAlipayInclude.setVisibility(View.VISIBLE);
                types = "2";
                imgYinlian.setImageResource(R.mipmap.checkboxunselected);
                imgAlipay.setImageResource(R.mipmap.checkboxselected);
                imgWx.setImageResource(R.mipmap.checkboxunselected);
                break;
            case R.id.layout_weixin:
                types = "3";
//                textTitle.setText("微信");
                layoutUnion.setVisibility(View.GONE);
                layoutAlipayInclude.setVisibility(View.GONE);
                imgYinlian.setImageResource(R.mipmap.checkboxunselected);
                imgAlipay.setImageResource(R.mipmap.checkboxunselected);
                imgWx.setImageResource(R.mipmap.checkboxselected);
                break;
            case R.id.editText:
                break;
            case R.id.textView2:
               /* // 单项选择
                for (int i = 0; i <= 10; i++) {
                    mList.add("item" + i);
                }*/

                Util.alertBottomWheelOption(WithDrawActivity.this, mList, new Util.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int postion) {
//                        Toast.makeText(WithDrawActivity.this, mList.get(postion), Toast.LENGTH_SHORT).show();

                        textView2.setText(mList.get(postion));
                    }
                });
                break;

            case R.id.img_yinlian:
//                textTitle.setText("银行卡");
                types = "1";
                layoutUnion.setVisibility(View.VISIBLE);
                layoutAlipayInclude.setVisibility(View.GONE);
                imgYinlian.setImageResource(R.mipmap.checkboxselected);
                imgAlipay.setImageResource(R.mipmap.checkboxunselected);
                imgWx.setImageResource(R.mipmap.checkboxunselected);
                break;
            case R.id.img_alipay:
//                textTitle.setText("支付宝");
                types = "2";
                layoutUnion.setVisibility(View.GONE);
                layoutAlipayInclude.setVisibility(View.VISIBLE);
                imgYinlian.setImageResource(R.mipmap.checkboxunselected);
                imgAlipay.setImageResource(R.mipmap.checkboxselected);
                imgWx.setImageResource(R.mipmap.checkboxunselected);
                break;
            case R.id.img_wx:
//                textTitle.setText("微信");
                types = "3";
                layoutUnion.setVisibility(View.GONE);
                layoutAlipayInclude.setVisibility(View.GONE);
                imgYinlian.setImageResource(R.mipmap.checkboxunselected);
                imgAlipay.setImageResource(R.mipmap.checkboxunselected);
                imgWx.setImageResource(R.mipmap.checkboxselected);
                break;
            case R.id.btn_submit:
                if (types.equals("1")) {
                    if (TextUtils.isEmpty(editMoney.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入提现金额", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editText.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入真实姓名", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(textKaihucity.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请选择开户城市", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(textView2.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请选择提现银行", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editCount.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入提现账户", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editRecount.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "确认账户不能为空", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!editCount.getText().toString().trim().equals(editRecount.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "两次输入的账户不同", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }else if (!checkBankCard(editCount.getText().toString().trim())){
                        LovelyToast.makeText(WithDrawActivity.this, "请输入正确的银行卡号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }


                    else if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入手机号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!UtilMatches.checkMobile(editPhone.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入正确的手机号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editCode.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "验证码不能为空", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!editCode.getText().toString().equals(myCode)) {
                        LovelyToast.makeText(WithDrawActivity.this, "验证码不正确", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editPwd.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "支付密码不能为空", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (getTypes.equals("2")) {
                        LovelyToast.makeText(WithDrawActivity.this, "请勾选鹿正堂组合协议", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else {
                        submit_withDrawData();
                    }
                } else if (types.equals("2")) {
                    if (TextUtils.isEmpty(editMoney.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入提现金额", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editText.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入真实姓名", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editTextAlipay.getText().toString().trim())) {
//                        editText_alipay
                        LovelyToast.makeText(WithDrawActivity.this, "请输入支付宝账号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editTextRealipay.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输确认支付宝账号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!editTextAlipay.getText().toString().trim().equals(editTextRealipay.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "两次输入的支付宝账号不同", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入手机号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!UtilMatches.checkMobile(editPhone.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入正确的手机号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editCode.getText().toString().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "验证码不能为空", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!editCode.getText().toString().equals(myCode)) {
                        LovelyToast.makeText(WithDrawActivity.this, "验证码不正确", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editPwd.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "支付密码不能为空", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (getTypes.equals("2")) {
                        LovelyToast.makeText(WithDrawActivity.this, "请勾选鹿正堂组合协议", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else {
                        submit_withDrawData();
                    }
                } else {
                    if (TextUtils.isEmpty(editMoney.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入提现金额", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editText.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入真实姓名", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入手机号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!UtilMatches.checkMobile(editPhone.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "请输入正确的手机号", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editCode.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "验证码不能为空", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (!editCode.getText().toString().equals(myCode)) {
                        LovelyToast.makeText(WithDrawActivity.this, "验证码不正确", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (TextUtils.isEmpty(editPwd.getText().toString().trim())) {
                        LovelyToast.makeText(WithDrawActivity.this, "支付密码不能为空", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else if (getTypes.equals("2")) {
                        LovelyToast.makeText(WithDrawActivity.this, "请勾选鹿正堂组合协议", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else {
                        submit_withDrawData();
                    }
                }


                break;

            case R.id.text_sendbtn:

                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(WithDrawActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(WithDrawActivity.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(WithDrawActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    getVerificationCode();
                }
                break;
            //组合协议
            case R.id.text_zuhe:
                startActivity(new Intent(WithDrawActivity.this, ZuHeActivity.class));

                break;
            //退款
            case R.id.layout_city:

                new EasySideBarBuilder(WithDrawActivity.this)
                        .setTitle("城市选择")
                        /*.setIndexColor(Color.BLUE)*/
                        .setIndexColor(0xFF0095EE)
                       /* .isLazyRespond(true) //懒加载模式*/
//                        .setHotCityList(hotCityList)//热门城市列表
//                        .setIndexItems(mIndexItems)//索引字母
//                        .setLocationCity("广州")//定位城市
                        .setMaxOffset(60)//索引的最大偏移量
                        .start();

                break;
            //退款
            case R.id.text_kaihucity:

                new EasySideBarBuilder(WithDrawActivity.this)
                        .setTitle("城市选择")
                        /*.setIndexColor(Color.BLUE)*/
                        .setIndexColor(0xFF0095EE)
                       /* .isLazyRespond(true) //懒加载模式*/
//                        .setHotCityList(hotCityList)//热门城市列表
//                        .setIndexItems(mIndexItems)//索引字母
//                        .setLocationCity("广州")//定位城市
                        .setMaxOffset(60)//索引的最大偏移量
                        .start();

                break;
            case R.id.text_withdraw_record:
                startActivity(new Intent(WithDrawActivity.this, WithDrawRecordActivity.class));

                break;


        }
    }

    //数据回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case EasySideBarBuilder.CODE_SIDEREQUEST:
                if (data != null) {
                    city = data.getStringExtra("selected");
/*                    Toast.makeText(this, "选择的城市：" + city, Toast.LENGTH_SHORT).show();*/
                    textKaihucity.setText(city);
                }
                break;

            default:
                break;
        }

    }

    public void getVerificationCode() {
        new ConnVerificationCode2(editPhone.getText().toString().trim(), "tong", BaseApplication.basePreferences.readUid(), new AsyCallBack<VerificationCodeBean>() {

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {
               /* countTime.start();

                myCode = registerBean.getData().getCode() + "";
                super.onSuccess(toast, type, o, registerBean);*/
                if (registerBean.getCode() == 200) {
                    countTime.start();
//                finish();myCode=registerBean.getData().getCode();
                    LovelyToast.makeText(WithDrawActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    myCode = registerBean.getData().getCode() + "";
                } else {
                    LovelyToast.makeText(WithDrawActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
               /* LovelyToast.makeText(RegisterActivity.this, ConnVerificationCode.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/

                textSendbtn.setEnabled(true);
                textSendbtn.setText("获取验证码");
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

            }
        }


        ).execute(this);
    }

    public void getTixianPersonalData() {
        new ConnWithDrawBank(BaseApplication.basePreferences.readUid(), new AsyCallBack<WithDrawBankBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, WithDrawBankBean withDrawBankBean) throws Exception {
                super.onSuccess(toast, type, o, withDrawBankBean);
                textMoney.setText("可提现金额￥" + withDrawBankBean.getBalance());

//                mList.addAll(withDrawBankBean.getData());
                dataBeanList.addAll(withDrawBankBean.getData());
                for (int i = 0; i < dataBeanList.size(); i++) {
                    mList.add(dataBeanList.get(i).getBanksname());
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(WithDrawActivity.this, false);
    }


    public void submit_withDrawData() {
        new ConnWithDrawSubmit(BaseApplication.basePreferences.readUid(), types, editMoney.getText().toString(), editText.getText().toString(), city, textView2.getText().toString(), textView2.getText().toString(), editCount.getText().toString(), editPhone.getText().toString(), editTextAlipay.getText().toString(), editPwd.getText().toString(), new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                if (commenBean.getCode() == 200) {
                    LovelyToast.makeText(WithDrawActivity.this, commenBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    finish();
                } else {
                    LovelyToast.makeText(WithDrawActivity.this, commenBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(WithDrawActivity.this, false);
    }



    /*
    校验过程：
    1、从卡号最后一位数字开始，逆向将奇数位(1、3、5等等)相加。
    2、从卡号最后一位数字开始，逆向将偶数位数字，先乘以2（如果乘积为两位数，将个位十位数字相加，即将其减去9），再求和。
    3、将奇数位总和加上偶数位总和，结果应该可以被10整除。
    */
    /**
     * 校验银行卡卡号
     */
    public static boolean checkBankCard(String bankCard) {
        if(bankCard.length() < 15 || bankCard.length() > 19) {
            return false;
        }
        char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
        if(bit == 'N'){
            return false;
        }
        return bankCard.charAt(bankCard.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     * @param nonCheckCodeBankCard
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeBankCard){
        if(nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
                || !nonCheckCodeBankCard.matches("\\d+")) {
            //如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeBankCard.trim().toCharArray();
        int luhmSum = 0;
        for(int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if(j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char)((10 - luhmSum % 10) + '0');
    }


}
